package com.qlk.odcinterface.controller;
   

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qlk.odcinterface.controller.base.BaseController;
import com.qlk.odcinterface.domain.PmsCouponChannel;
import com.qlk.odcinterface.domain.query.PmsCouponChannelQuery;
import com.qlk.odcinterface.service.PmsCouponChannelService;
import com.qlk.odcinterface.utils.exceptions.ExistedException;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;

/**
 * PmsCouponChannelController ：满减促销渠道对应表控制器
 * 
 * @author zhaoyanan
 * @version 2016-4-14 14:11:37
*/
@Controller
@RequestMapping("pmsCouponChannel")
public class PmsCouponChannelController extends BaseController {

    @Autowired
    private PmsCouponChannelService pmsCouponChannelService;

    /** 视图前缀 */
    private static final String viewPrefix ="pmsCouponChannel";
    
    private static final Log LOG = LogFactory.getLog(PmsCouponChannelController.class);

    /**
     * 分页 查询数据
     * 
     * @param model
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "")
    public String index(Model model, PageUtil page, PmsCouponChannelQuery query) {
        try {
            List<PmsCouponChannel> dataList = pmsCouponChannelService.queryPmsCouponChannelListWithPage(query, page);
            model.addAttribute("dataList", dataList);// 数据集合
            model.addAttribute("query", query);// 查询参数
            model.addAttribute("page", page);// 分页
        } catch (Exception e) {
            LOG.error("pmsCouponChannel index has error.", e);
        }
        return viewPrefix + "/index";
    }

    /**
     * 满减促销渠道对应表----添加跳转
     * 
     * @return
     */
    @RequestMapping(value = "addForward")
    public String addForward() {
        return viewPrefix + "/add";
    }

    /**
     * 满减促销渠道对应表----添加
     * 
     * @param pmsCouponChannel
     * @return
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public Wrapper<?> add(PmsCouponChannel pmsCouponChannel) {
        try {
//            pmsCouponChannel.setCreateUser(getLoginUserCnName());
            if (pmsCouponChannelService.insert(pmsCouponChannel)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
            }
        } catch (ExistedException e) {
            LOG.warn("pmsCouponChannel add fail, exist pmsCouponChannel.");
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败，已经存在");
        } catch (Exception e) {
            LOG.error("pmsCouponChannel add has error.", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
        }
    }

    /**
     * 满减促销渠道对应表----更新跳转
     * 
     * @param model
     * @param pmsCouponChannel
     * @return
     */
    @RequestMapping(value = "updateForward")
    public String updateForward(Model model, PmsCouponChannel pmsCouponChannel) {
        try {
            PmsCouponChannel pmsCouponChannelResult = pmsCouponChannelService.getPmsCouponChannelById(pmsCouponChannel.getId());
            model.addAttribute("pmsCouponChannel", pmsCouponChannelResult);
        } catch (Exception e) {
            LOG.error("pmsCouponChannel updateForward has error.", e);
        }
        return viewPrefix + "/update";
    }

    /**
     * 满减促销渠道对应表----更新
     * 
     * @param model
     * @param pmsCouponChannel
     * @return
     */
    @RequestMapping(value = "update")
    @ResponseBody
    public Wrapper<?> update(Model model, PmsCouponChannel pmsCouponChannel) {
        try {
//            pmsCouponChannel.setUpdateUser(getLoginUserCnName());
            if (pmsCouponChannelService.update(pmsCouponChannel)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "更新成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "更新失败！");
            }
        } catch (Exception e) {
            LOG.error("pmsCouponChannel update has error.", e);
            return WrapMapper.error();
        }
    }

    /**
     * 满减促销渠道对应表----删除
     * 
     * @param pmsCouponChannel
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public Wrapper<?> delete(PmsCouponChannel pmsCouponChannel) {
        try {
//            pmsCouponChannel.setUpdateUser(getLoginUserCnName());
            if (pmsCouponChannelService.delete(pmsCouponChannel)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败！");
            }
        } catch (Exception e) {
            LOG.error("pmsCouponChannel delete has error.", e);
            return WrapMapper.error();
        }
    }

    /**
     * 满减促销渠道对应表----查询-无分页
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "query")
    @ResponseBody
    public Wrapper<?> query(PmsCouponChannelQuery query) {
        try {
            List<PmsCouponChannel> list = pmsCouponChannelService.queryPmsCouponChannelList(query);
            if (!CollectionUtils.isEmpty(list)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询配置信息失败！");
            }
        } catch (Exception e) {
            LOG.error("pmsCouponChannel query has error.", e);
            return WrapMapper.error();
        }
    }
    
    /**
     * 查询满减促销渠道对应表详情
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    @ResponseBody
    public Wrapper<?> detail(PmsCouponChannelQuery query) {
        if (null == query || null == query.getId()) {
            return illegalArgument();
        }

        try {
            PmsCouponChannel pmsCouponChannel = pmsCouponChannelService.getPmsCouponChannelById(query.getId());
            if (pmsCouponChannel != null) {
                return new Wrapper<PmsCouponChannel>().result(pmsCouponChannel);
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询满减促销渠道对应表详情失败！");
            }
        } catch (Exception e) {
            LOG.warn("detail pmsCouponChannel has error.", e);
            return error();
        }
    }
}
